package com.yyfax.ph.personal.company;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.View;
import butterknife.BindView;
import butterknife.OnClick;
import com.yyfax.common.form.FormRow;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.R;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.BaseYYFaxActivity;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.personal.settings.GetCompanyModel;

/**
 * 我的渠道
 * Created by zhangxian on 2017/11/4.<br>
 * Copyright 2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class BindCompanyActivity extends BaseYYFaxActivity implements BindCompanyPresenter.View {
	private static final int LOADER_ID = 401;
	private static final String COMPANY = "company";
	@BindView(R.id.company_input_fr)
	FormRow mCompanyFr;

	private BindCompanyPresenter mPresenter;

	public static void start(Activity aty) {
		Intent intent = new Intent(aty, BindCompanyActivity.class);
		aty.startActivity(intent);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return new PresenterLoader<>(this, new BindCompanyPresenter.Generator());
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		mPresenter = (BindCompanyPresenter) data;
		mPresenter.attach(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		getSupportLoaderManager().destroyLoader(LOADER_ID);
	}

	@Override
	protected void onStart() {
		super.onStart();
		getSupportLoaderManager().initLoader(LOADER_ID, null, this);
	}

	@Override
	public int getContentViewRes() {
		return R.layout.bind_company_activity;
	}

	@Override
	public void initContentView() {
		super.initContentView();
		setBtnRightVisible(false);
		setTitleLeftVisible(false);
		setTitleVisible(true);
		setTitle(R.string.my_channel);
	}


	@Override
	public void onBackBtnPressed() {
		super.onBackBtnPressed();
		finish();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

	@Override
	public void bindCompanySuccess(String tips) {
		dismissLoading();
		Intent intent = new Intent(this, ShowBindCompanyActivity.class);
		intent.putExtra(ShowBindCompanyActivity.COMPANY, "");
		intent.putExtra(ShowBindCompanyActivity.STATUS, GetCompanyModel.STATUS_NONE);
		intent.putExtra(ShowBindCompanyActivity.PHONE, "");
		ShowBindCompanyActivity.start(this, intent);
		finish();
	}

	@Override
	public void unbindCompanySuccess(String tips) {
		dismissLoading();
		finish();
	}

	@Override
	public void bindCompanyFailure(String tips) {
		dismissLoading();
		showToast(tips);
	}

	@OnClick({R.id.bind_company_tv, R.id.what_is_channel_tv})
	public void onClick(View view) {
		super.onClick(view);
		switch (view.getId()) {
			case R.id.bind_company_tv:
				String channel = mCompanyFr.getText().toString();
				if (!TextUtils.isEmpty(channel)) {
					YJPH.hideInputWindow(BindCompanyActivity.this);//隐藏键盘
					mPresenter.bindCompany(mCompanyFr.getText().toString());
				} else {
					showToast(String.format(getString(R.string.please_input_toast), getString(R.string.channel_number_tips)));
				}
				break;
			case R.id.what_is_channel_tv:
				StatService.onEvent(YJPH.getInstance(), StatIDSet.EVENT_ID_E034);
				showTipsDialog(getString(R.string.channel_number_tips), getString(R.string.what_is_channel_number_description), true);
				break;
			default:
				break;
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString(COMPANY, mCompanyFr.getText().toString());

	}

	@Override
	public void restoreInstanceState(Bundle savedInstanceState) {
		super.restoreInstanceState(savedInstanceState);
		if (savedInstanceState != null) {
			mCompanyFr.setText(savedInstanceState.getString(COMPANY));
		}
	}
}
